﻿using System;
using System.Collections.Generic;
using System.Text;
using Lswweb.Model;
using Lswweb.Domain;
using Lswweb.IDAL;
using Lswweb.Common;

namespace Lswweb.Domain
{
    public class Products
    {
        private static IProducts productDAL = DALFactory.CreateProductDAL();

        #region 查询

        public static ProductInfo GetProduct(int productId)
        {
            if (productId <= 0)
                return null;

            return productDAL.GetProduct(productId);
        }

        public static IList<ProductInfo> GetProducts(string searchKey, SearchKeyType searchKeyType, int categoryId, Boolean? isWholesale, StatusType? type, AuditingStatusType? auditingStatus, bool? isHot, bool? isTop, Pair<DateTime?> dateRange, Pair<Decimal?> priceRange, string orderBy, int pageIndex, int pageSize, out int totalRecord)
        {
            return productDAL.GetProducts(searchKey, searchKeyType, categoryId, isWholesale, type, auditingStatus, isHot, isTop, dateRange, priceRange, orderBy, SiteContext.Current.Language, pageIndex, pageSize, out totalRecord);
        }

        #endregion

        #region 更新

        public static void UpdateDisplayOrder(int productId, int displayOrder)
        {
            if (productId <= 0)
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);
                return;
            }

            string url = SiteContext.Current.ReferrerUrl;
            if (productDAL.UpdateDisplayOrder(productId, displayOrder))
                SiteContext.RenderMessage(ExceptionType.HandleSuccess, url);
            else
                SiteContext.RenderMessage(ExceptionType.UnknownError, url);
        }

        public static void Update(ProductInfo productInfo)
        {
            productInfo = NewsHandler(productInfo);

            if (productDAL.Update(productInfo))
            {
                if (!string.IsNullOrEmpty(productInfo.ImageUrl))
                    productDAL.UpdateImageUrl(productInfo.Id, productInfo.ImageUrl);
                SiteContext.RenderMessage(ExceptionType.HandleSuccess);
            }
            else
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);
            }
        }

        public static void UpdateIsHot(int id, bool isHot)
        {
            if (id <= 0)
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);
                return;
            }

            string url = SiteContext.Current.Context.Request.UrlReferrer.PathAndQuery;
            if (productDAL.UpdateIsHot(id, isHot))
                SiteContext.RenderMessage(ExceptionType.HandleSuccess, url);
            else
                SiteContext.RenderMessage(ExceptionType.UnknownError);
        }

        public static void UpdateIsTop(int id, bool isTop)
        {
            if (id <= 0)
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);
                return;
            }

            string url = SiteContext.Current.Context.Request.UrlReferrer.PathAndQuery;
            if (productDAL.UpdateIsTop(id, isTop))
                SiteContext.RenderMessage(ExceptionType.HandleSuccess, url);
            else
                SiteContext.RenderMessage(ExceptionType.UnknownError);
        }

        public static void UpdateState(int id, StatusType state)
        {
            if (id <= 0)
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);
                return;
            }

            string url = SiteContext.Current.Context.Request.UrlReferrer.PathAndQuery;
            if (productDAL.UpdateStatus(id, state))
                SiteContext.RenderMessage(ExceptionType.HandleSuccess, url);
            else
                SiteContext.RenderMessage(ExceptionType.UnknownError);
        }

        public static void AddSale(int productId, int step)
        {
            var product = productDAL.GetProduct(productId);
            if (product != null)
            {
                product.Sale += step;
                productDAL.Update(product);
            }

        }

        #endregion

        #region 添加/删除

        public static void Insert(ProductInfo productInfo)
        {
            productInfo = NewsHandler(productInfo);
            if (productDAL.Insert(productInfo) > 0)
                SiteContext.RenderMessage(ExceptionType.HandleSuccess);
            else
                SiteContext.RenderMessage(ExceptionType.UnknownError);
        }

        public static void Delete(int productId)
        {
            string returnUrl = SiteContext.Current.ReferrerUrl;
            ProductInfo info = GetProduct(productId);
            if (info == null)
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError, returnUrl);
                return;
            }

            ExceptionType type = ExceptionType.UnknownError;

            if (!string.IsNullOrEmpty(info.ImageUrl))
            {
                Globals.FileDelete(ImageHelper.GetThumbnailUrl(info.ImageUrl, false));

                Globals.FileDelete(info.ImageUrl);
            }

            if (info.ImageFiles != null && info.ImageFiles.Count > 0)
            {
                foreach (UpFileInfo file in info.ImageFiles)
                {
                    UpFiles.Delete(file.Id);
                }
            }

            if (productDAL.Delete(productId))
                type = ExceptionType.HandleSuccess;
            SiteContext.RenderMessage(type, returnUrl);
        }

        #endregion

        public static string GetCategoryName()
        {
            return GetCategoryName(SiteContext.Current.CategoryId);
        }
        public static string GetCategoryName(int categoryId)
        {
            TreeTables treeTable = TreeTables.ProductCategories;
            return treeTable.GetCategoryName(categoryId);
        }

        public static string GetNextUrl(int categoryId, int productId)
        {
            ProductInfo info = productDAL.GetNextProduct(productId, categoryId);
            if (info != null)
            {
                return SiteUrls.Urls.ProductShow(categoryId, info.Id);
            }
            else
            {
                return string.Empty;
            }
        }
        public static string GetNextUrl()
        {
            return GetNextUrl(SiteContext.Current.CategoryId, SiteContext.Current.Id);
        }

        public static string GetPrevUrl(int categoryId, int productId)
        {
            ProductInfo info = productDAL.GetPrevProduct(productId, categoryId);
            if (info != null)
            {
                return SiteUrls.Urls.ProductShow(categoryId, info.Id);
            }
            else
            {
                return string.Empty;
            }
        }
        public static string GetPrevUrl()
        {
            return GetPrevUrl(SiteContext.Current.CategoryId, SiteContext.Current.Id);
        }

        private static ProductInfo NewsHandler(ProductInfo productInfo)
        {
            if (productInfo == null)
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);
                return null;
            }

            if (productInfo.Id <= 0 && SiteContext.Current.Id > 0)
                productInfo.Id = SiteContext.Current.Id;

            productInfo.Content = ImageHelper.MoveEditorImagesToDirectory(SiteModule.Product, productInfo.Content);
            productInfo.ImageUrl = ImageHelper.MoveImagesToDirectory(SiteModule.Product, productInfo.ImageUrl);

            if (string.IsNullOrEmpty(productInfo.Ver))
                productInfo.Ver = SiteContext.Current.Language;

            return productInfo;
        }

        public static void AddImage(int productId,int upFileId)
        {
            productDAL.AddImage(productId, upFileId);
        }

        #region Commentary

        private static ICommentary commentaryDAL = DALFactory.CreateCommentary();

        public static CommentaryInfo GetCommentary(int commentaryId)
        {
            return commentaryDAL.GetCommentary(commentaryId);
        }

        public static IList<CommentaryInfo> GetCommentaries(int productId, int pageIndex, int pageSize, out int totalRecord)
        {
            return commentaryDAL.GetCommentary(productId,pageIndex,pageSize,out totalRecord);
        }

        public static void AddCommentary(CommentaryInfo commentary)
        {
            if (commentaryDAL.Insert(commentary) > 0)
                SiteContext.RenderMessage(ExceptionType.HandleSuccess);
            else
                SiteContext.RenderMessage(ExceptionType.UnknownError);
        }

        public static void DeleteCommentary(int commentaryId)
        {
            if (commentaryDAL.Delete(commentaryId))
                SiteContext.RenderMessage(ExceptionType.HandleSuccess);
            else
                SiteContext.RenderMessage(ExceptionType.UnknownError);
        }

        public static void UpdateCommentary(CommentaryInfo commentary)
        {
            if (commentaryDAL.Update(commentary))
                SiteContext.RenderMessage(ExceptionType.HandleSuccess);
            else
                SiteContext.RenderMessage(ExceptionType.UnknownError);
        }

        public static IList<CommentaryInfo> GetCommentaryByPrdocutCategory(int productCategoryId, int pageIndex, int pageSize, out int totalRecord)
        {
            return commentaryDAL.GetCommentaryByPrdocutCategory(productCategoryId, pageIndex, pageSize, out totalRecord);
        }

        public static IList<CommentaryInfo> GetCommentaryByUser(int userId, int pageIndex, int pageSize, out int totalRecord)
        {
            return commentaryDAL.GetCommentaryByUser(userId, pageIndex, pageSize, out totalRecord);
        }

        public static IList<CommentaryInfo> GetCommentaries(bool? isBack, int pageIndex, int pageSize, out int totalRecord)
        {
            return commentaryDAL.GetCommentary(isBack, pageIndex, pageSize, out totalRecord);
        }

        #endregion
    }
}
